//package com.enze.springbootmail;
//
///**
// * @Author TacoMan
// * @Date 2023/6/6 9:27
// * @Version 1.0
// */
//public class HuaWeiSendSms {
//    import com.huawei.apig.sdk.util.Constant;
//import com.huawei.apig.sdk.util.SSLCipherSuiteUtil;
//
//import com.cloud.apigateway.sdk.utils.Client;
//import com.cloud.apigateway.sdk.utils.Request;
//
//import org.apache.http.Header;
//import org.apache.http.HttpEntity;
//import org.apache.http.HttpResponse;
//import org.apache.http.client.methods.HttpRequestBase;
//import org.apache.http.impl.client.CloseableHttpClient;
//import org.apache.http.util.EntityUtils;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
//import java.io.UnsupportedEncodingException;
//import java.net.URLEncoder;
//import java.util.Arrays;
//
//    public class SendSms {
//
//        private static final Logger LOGGER = LoggerFactory.getLogger(SendSms.class);
//
//        public static final String UTF_8 = "UTF-8";
//
//        public static void main(String[] args) throws Exception {
//            //必填,请参考"开发准备"获取如下数据,替换为实际值
//            String url = "https://smsapi.cn-north-4.myhuaweicloud.com:443/sms/batchSendSms/v1"; //APP接入地址(在控制台"应用管理"页面获取)+接口访问URI
//            String appKey = "c8RWg3ggEcyd4D3p94bf3Y7x1Ile"; //APP_Key
//            String appSecret = "q4Ii87BhST9vcs8wvrzN80SfD7Al"; //APP_Secret
//            String sender = "889988990000002"; //国内短信签名通道号或国际/港澳台短信通道号
//            String templateId = "8ff55eac1d0b478ab3c06c3c6a492300"; //模板ID
//
//            //条件必填,国内短信关注,当templateId指定的模板类型为通用模板时生效且必填,必须是已审核通过的,与模板类型一致的签名名称
//            //国际/港澳台短信不用关注该参数
//            String signature = "华为云短信测试"; //签名名称
//
//            //必填,全局号码格式(包含国家码),示例:+86151****6789,多个号码之间用英文逗号分隔
//            String receiver = "+86151****6788,+86151****6789"; //短信接收人号码
//
//            //选填,短信状态报告接收地址,推荐使用域名,为空或者不填表示不接收状态报告
//            String statusCallBack = "https://your.server.com/rest/callback/statusReport";
//
//            /**
//             * 选填,使用无变量模板时请赋空值 String templateParas = "";
//             * 单变量模板示例:模板内容为"您的验证码是${1}"时,templateParas可填写为"[\"369751\"]"
//             * 双变量模板示例:模板内容为"您有${1}件快递请到${2}领取"时,templateParas可填写为"[\"3\",\"人民公园正门\"]"
//             * 模板中的每个变量都必须赋值，且取值不能为空
//             * 查看更多模板和变量规范:产品介绍>模板和变量规范
//             */
//            String templateParas
//                    = "[\"369751\"]"; //模板变量，此处以单变量验证码短信为例，请客户自行生成6位验证码，并定义为字符串类型，以杜绝首位0丢失的问题（例如：002569变成了2569）。
//
//            //请求Body,不携带签名名称时,signature请填null
//            String body = buildRequestBody(sender, receiver, templateId, templateParas, statusCallBack, signature);
//            if (null == body || body.isEmpty()) {
//                LOGGER.warn("body is null.");
//                return;
//            }
//
//            Request request = new Request();
//            request.setKey(appKey);
//            request.setSecret(appSecret);
//            request.setMethod("POST");
//            request.setUrl(url);
//            request.addHeader("Content-Type", "application/x-www-form-urlencoded");
//            request.setBody(body);
//            LOGGER.info("Print the body: {}", body);
//
//            CloseableHttpClient client = null;
//            try {
//                HttpRequestBase signedRequest = Client.sign(request, Constant.SIGNATURE_ALGORITHM_SDK_HMAC_SHA256);
//                LOGGER.info("Print the authorization: {}", Arrays.toString(signedRequest.getHeaders("Authorization")));
//                Header[] requestAllHeaders = signedRequest.getAllHeaders();
//                for (Header h : requestAllHeaders) {
//                    LOGGER.info("req Header with name: {} and value: {}", h.getName(), h.getValue());
//                }
//
//                // 为防止因HTTPS证书认证失败造成API调用失败,需要先忽略证书信任问题
//                client = (CloseableHttpClient) SSLCipherSuiteUtil.createHttpClient(Constant.INTERNATIONAL_PROTOCOL);
//                HttpResponse response = client.execute(signedRequest);
//
//                LOGGER.info("Print the status line of the response: {}", response.getStatusLine().toString());
//                Header[] resHeaders = response.getAllHeaders();
//                for (Header h : resHeaders) {
//                    LOGGER.info("Processing Header with name: {} and value: {}", h.getName(), h.getValue());
//                }
//                HttpEntity resEntity = response.getEntity();
//                if (resEntity != null) {
//                    LOGGER.info("Processing Body with name: {} and value: {}", System.getProperty("line.separator"),
//                            EntityUtils.toString(resEntity, "UTF-8"));
//                }
//            } catch (Exception e) {
//                LOGGER.error(e.getMessage());
//            } finally {
//                if (client != null) {
//                    client.close();
//                }
//            }
//        }
//
//        static String buildRequestBody(String sender, String receiver, String templateId, String templateParas,
//                                       String statusCallBack, String signature) throws UnsupportedEncodingException {
//            if (null == sender || null == receiver || null == templateId || sender.isEmpty() || receiver.isEmpty()
//                    || templateId.isEmpty()) {
//                System.out.println("buildRequestBody(): sender, receiver or templateId is null.");
//                return null;
//            }
//
//            StringBuilder body = new StringBuilder();
//            appendToBody(body, "from=", sender);
//            appendToBody(body, "&to=", receiver);
//            appendToBody(body, "&templateId=", templateId);
//            appendToBody(body, "&templateParas=", templateParas);
//            appendToBody(body, "&statusCallback=", statusCallBack);
//            appendToBody(body, "&signature=", signature);
//            return body.toString();
//        }
//
//        private static void appendToBody(StringBuilder body, String key, String val) throws UnsupportedEncodingException {
//            if (null != val && !val.isEmpty()) {
//                LOGGER.info("Print appendToBody: {}:{}", key, val);
//                body.append(key).append(URLEncoder.encode(val, UTF_8));
//            }
//        }
//    }
//
//}
